<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <c:set var="ctx" value="${pageContext.request.contextPath}"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>结清审核页面</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <style>
        span.sq-button-round {
            height: 23px !important;
        }
        i.ico_flasher {
            height: 29px;
            vertical-align: middle;
        }
    </style>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div id="pageInTab" class="pageInTab">
        <form id="formSet1">
            <div class="fieldset1 tabel_col clearFix">
                <div class="fieldBox col_50">
                    <div class="label">融资租赁合同号</div>
                    <div class="field">
                        ${pay.lsCntNo}
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">承租人</div>
                    <div class="field">
                        ${pay.cstNm}
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">支付表号</div>
                    <div class="field">
                        <a href="javascript:detailBtn()" class="link">
                            ${pay.paySchNo}
                        </a>
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">结清方式</div>
                    <div class="field">
                        <span id="settleWay" name="settleWay">${pay.settleTypeNm}</span>
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">开始期次</div>
                    <div class="field">
                        ${pay.settlePd == 0?'到期':pay.settlePd}
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label" role="ligertip">自结清期应还租金合计</div>
                    <div class="field">
                        ${pay.surplusRentTotalStl}
                    </div>
                </div>

                <div class="fieldBox col_50">
                    <div class="label" role="ligertip">结清开始前每期应还租金合计</div>
                    <div class="field">
                        ${pay.rentBeforeSettleTotal}
                    </div>
                </div>

                <div class="fieldBox col_50">
                    <div class="label">核算金额校验</div>
                    <div class="field">
                        ${pay.checkbitAmt}
                    </div>
                </div>

                <div class="fieldBox col_100">
                    <div class="label">最终应还金额公式</div>
                    <div class="field">
                        <span style="color: red">结清前应还的每期租金合计 + 自结清期剩余租金合计 + 逾期罚息 + 其他费用 - 保证金 - 保险保证金 - 逾期当期已还款金额 + 未核销借款金额</span>
                    </div>
                </div>


                <div class="caption">
                    <span class="title">已付款项明细信息</span>
                </div>
                <div class="fieldBox col_100">
                    <div class="sqPanel">
                        <div id="ligergrid_fee" class="sq-grid"></div>
                    </div>
                    <div class="sqPanel">
                        <div id="ligergrid_blf" class="sq-grid sq-grid-noTitle"></div>
                    </div>
                </div>

                <div class="caption">
                    <c:if test="${ckControl == 1}">
                        <div class="btnsBox posL">
                            <span class="sq-button sq-button-round" onclick="toManageStlFeeDialog()"><i class="ico_flasher"></i>维护结清费用项</span>
                        </div>
                    </c:if>
                    <span class="title">结清款项信息</span>
                </div>
                <div class="fieldBox col_100">
                    <div class="sqPanel">
                        <div id="ligergrid_shouldPay" class="sq-grid sq-grid-noTitle"></div>
                    </div>
                </div>

                <div class="fieldBox col_100">
                    <div class="label">应收结清款</div>
                    <div class="field">
                        <span id="totalSettAmt"></span>
                    </div>
                </div>
            </div>

            <c:if test="${showBk_oCIn == 1 }">
                <jsp:include page="../../project/prjPrdPaySchBank/prjPrdPaySchBankAdd.jsp"></jsp:include>
            </c:if>
            <c:if test="${showBk_oCOut == 1 }">
                <jsp:include page="../../project/prjPrdPaySchBank/prjPrdPaySchBankAdd1.jsp"></jsp:include>
            </c:if>

            <%--附件--%>
            <jsp:include page="../../settlement/settleFile.jsp" flush="true">
                <jsp:param name="pkId" value="${pay.paySchId}"/>
                <jsp:param name="filePath" value="prjprdpayschm"/>
                <jsp:param name="optAuth" value="${fileAuth}"/>
            </jsp:include>

            <%--结清资金维护--%>
            <div id="settleBindFundDiv"></div>

            <!-- 客户开票页面 -->
            <div id="cstPrdPayBillDiv"></div>

        </form>
    </div>
</div>

<script type="text/javascript">
    var basePath = '<%=path%>';
    var paySchId = '${pay.paySchId}';
    var wsPaySchId = '${pay.wsPaySchId}';
    var pInstId = '${processInstanceId}';
    var settlePd = '${pay.settlePd}';
    var should_pay_grid = null;
    $(function () {
        setTotalSettAmt(${pay.totalSettleAmt});

        initFeeDataGrid();
        initBlfDataGrid();
        initShouldPayDataGrid();

        loadInvoiceModule();
        loadBindFundMoudle();
    });

    function setTotalSettAmt(value) {
        var html = '<div style="display: inline-block; font-size: 16px;">';
        if(value < 0){
            html += "<span style='color:red'>￥";
            html += FormatMoney((parseFloat(value)).toFixed(2));
            html += "</span>";
        } else {
            html += "￥" + FormatMoney((parseFloat(value)).toFixed(2));
        }
        html += '</div>';
        $("#totalSettAmt").html(html);
    }

    // 初始化费用项grid
    function initFeeDataGrid() {
        var feeListPage = JSON.parse('${feeListPage}');
        $("#ligergrid_fee").ligerGrid({
            data: feeListPage,
            columns: [
                {display: '项目', name: 'feeTypCdNm', minWidth: 140},
                {display: '应收金额', name: 'feeAmt', minWidth: 140, type: 'currency', align: 'right', render: renderAmt},
                {display: '已收金额', name: 'paidUpAmt', minWidth: 60, type: 'currency', align: 'right', render: renderAmt},
                {display: '未收金额', name: 'arAmt', minWidth: 60, type: 'currency', align: 'right', render: renderAmt}
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: 300,
            heightDiff: -2,
            isScroll: false,
            checkbox: false,
            rownumbers: true,
            allowUnSelectRow: false,
            alternatingRow: true
        });
    }

    // 初始化本利罚grid
    function initBlfDataGrid() {
        var blfListPage = JSON.parse('${blfListPage}');
        $("#ligergrid_blf").ligerGrid({
            data: blfListPage,
            columns: [
                {display: '项目', name: 'feeTypCdNm', minWidth: 140,
                    render: function (rowdata, rowindex, value) {
                        if (rowdata.checkbit == 1) {
                            return '<div style="color: #f02b2b;">'+value+'</div>';
                        }
                        return value;
                    }
                },
                {display: '应收金额', name: 'feeAmt', minWidth: 140, render: renderAmt, type: 'currency', align: 'right',
                    totalSummary: {
                        render: function (suminf, column, cell) {
                            return '<div>合计：￥' + FormatMoney((parseFloat(suminf.sum)).toFixed(2)) + '</div>'
                        }
                    }
                },
                {display: '已收金额', name: 'paidUpAmt', minWidth: 60, render: renderAmt, type: 'currency', align: 'right',
                    totalSummary: {
                        render: function (suminf, column, cell) {
                            return '<div>合计：￥' + FormatMoney((parseFloat(suminf.sum)).toFixed(2)) + '</div>';
                        }
                    }
                },
                {display: '未收金额', name: 'arAmt', minWidth: 60, render: renderAmt, type: 'currency', align: 'right',
                    totalSummary: {
                        render: function (suminf, column, cell) {
                            return '<div>合计：￥' + FormatMoney((parseFloat(suminf.sum)).toFixed(2)) + '</div>';
                        }
                    }
                }
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            heightDiff: -2,
            isScroll: false,
            checkbox: false,
            rownumbers: true,
            frozen: false,
            allowUnSelectRow: false,
            alternatingRow: true
        });
    }

    // 初始化应还grid
    function initShouldPayDataGrid() {
        should_pay_grid = $("#ligergrid_shouldPay").ligerGrid({
            url: basePath + '/settlement/fee/initSettleItemList.do?pInstId=' + pInstId,
            method: 'get',
            columns: [
                {display: '费用科目', name: 'feeTypCdNm', minWidth: 140},
                {
                    display: '金额', name: 'feeAmt', minWidth: 140, align: 'right',
                    render: function (item) {
                        var amt = item.feeAmt;
                        if (item.capFlow == ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT) {
                            amt = "<span style='color:red'>" +
                                "￥" + FormatMoney((parseFloat(item.feeAmt)).toFixed(2))
                                + "</span>";
                        } else {
                            amt = "￥" + FormatMoney((parseFloat(item.feeAmt)).toFixed(2));
                        }
                        return amt;
                    }
                }
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            heightDiff: -2,
            isScroll: false,
            checkbox: false,
            frozen: false,
            allowUnSelectRow: false,
            alternatingRow: true,
            groupColumnName: 'capFlow',
            groupRender: function (capFlow, groupdata) {
                if (capFlow == ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT) {
                    return "<span style='font-size: 14px;'> 应退款项</span>";
                } else if (capFlow == ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_IN) {
                    return "<span style='font-size: 14px;'> 应收款项</span>";
                } else {
                    return capFlow;
                }
            },
            onError: function(XMLHttpRequest, textStatus, errorThrown){
                $.ligerDialog.SQalert({ content: "加载结清款项列表失败！", SQ_type: "error", SQ_showButtons: true });
            }
        });
    }

    //格式化为人民币格式
    function renderAmt(rowdata, rowindex, value) {
        if (null !== value && value !== 'undefined' && value !== "") {
            if (typeof Number(value) == 'number') {
                var h = "￥" + FormatMoney((parseFloat(value)).toFixed(2));
                return h;
            }
        }
    }

    /******************************************/
    // 结清费用项管理
    function toManageStlFeeDialog() {
        var onCancel = function (item, Dialog) {
            Dialog.hide();
            should_pay_grid.reload();
            refreshSettleAmt();
        };
        var title = "维护结清费用信息";
        $.ligerDialog.open({
            width: 650,
            height: 400,
            showMax: false,
            allowClose: false,
            title: title,
            url: basePath + '/settlement/fee/manageListPage.do?paySchId=' + wsPaySchId + '&pInstId=' + pInstId,
            buttons: [{
                text: "关闭", type: 'cancel', onclick: onCancel, cls: 'l-dialog-btn-no'
            }]
        });
    }

    // 刷新结清款
    function refreshSettleAmt() {
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: {paySchId: paySchId},
            url: basePath + '/settlement/bill/refreshSettleAmt.do',
            beforeSend: function () {
                wait_dialog = top.$.ligerDialog.SQalert({ content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting" });
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    setTotalSettAmt(msg.data);
                } else {
                    $("#totalSettAmt").html('获取失败，<a class="sq-button-hasicon" href="javascript:refreshSettleAmt()"><i class="sqBtnIco_28"></i>刷新</a>');
                }
            }
        });
    }

    function loadBindFundMoudle() {
        var bindFund = '${bindFund}';
        if (bindFund != '1') {
            return;
        }
        var params = {
            paySchId: paySchId,
            wsPaySchId: wsPaySchId
        };
        $("#settleBindFundDiv").load(basePath + "/settlement/fund/toBindFundPage.do", params, function (responseText, textStatus) {
        });
    }

    //初始化开票页面
    function loadInvoiceModule() {
        var optba = '${optba}';
        var optbf = '${optbf}';
        var optbd = '${optbd}';
        var _url = "";
        if (optba == 1) {
            _url = "/customer/external/toClearPayPrjBillAppLyListPage.do";
        } else if (optbf == 1) {
            _url = "/customer/external/toClearPayCstInvoiceFinanceListPage.do";
        } else if (optbd == 1) {
            _url = "/customer/external/toClearPayCstInvoiceDetailListPage.do";
        }
        if (StringUtils.isNotEmpty(_url)) {
            var _extPaySchMId = '${paySchId}';
            var _extPrjId = '${pay.prjId }';
            var _cstId = '${pay.cstId }';
            var _cstNm = '${pay.cstNm }';
            var parms = {
                extPaySchMId: _extPaySchMId,
                extPrjId: _extPrjId,
                cstId: _cstId,
                cstNm: _cstNm,
                procInstId: pInstId
            };
            //如果有参数则在第二个参数中传入
            $("#cstPrdPayBillDiv").load(basePath + _url, parms, function (responseText, textStatus) {
            });
        }
    }

    function detailBtn() {
        var prjId = '${pay.prjId}';
        var prjPrdDtlId = '${pay.prjPrdDtlId}';
        var prjPrdId = '${pay.prjPrdId}';
        var paySchId = '${pay.paySchId}';
        Add_navTab('tab_prj_quat_prps' + paySchId, '查看支付表', basePath + '/project/prjprdpayschm/toAddPage.do?prjId=' + prjId + '&&id=' + paySchId + '&&prjPrdDtlId=' + prjPrdDtlId + '&&prjPrdId=' + prjPrdId + '&&detailFlag=true');
    }

</script>
</body>
</html>
